home *** CD-ROM | disk | FTP | other *** search
- Path: news.Stanford.EDU!usenet
- From: brien@leland.stanford.edu (brien oberstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Copy constructing an already default constructed object
- Date: Sat, 27 Jan 1996 18:47:07 GMT
- Organization: Stanford University
- Message-ID: <310a717f.5074136@nntp>
- References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com> <3108ef14.340699@nntp> <4ebmst$7cl@dawn.mmm.com>
- NNTP-Posting-Host: tip-mp9-ncs-15.stanford.edu
-
- On 26 Jan 1996 23:08:13 GMT, kjhopps@mmm.com (Kevin J Hopps) wrote:
-
- >Personally, I find the assignment operator almost exactly as
- >"painful" to write as the copy constructor. But if for some
- >reason it is more painful to initialize objects in operator=
- >than in the copy constructor, you could write operator= this way:
- > void A::operator=(const A& rhs)
- > {
- > if (this != &rhs) {
- > this->~A();
- > new (this) A(rhs);
- > }
- > }
- >
- >But for two members, it's the same number of lines anyway :-)
-
- Beautiful! This is exactly what I was looking for. Is this
- completely safe? Are there any situations I to watch out for when
- using this construct? I've never used templates, but I think that
- they would be perfect for this.
-
- -brien
-